using UnityEngine;
using System.Collections;

public class VampiroControl : StateBehaviour<VampiroControl> 
{
    public float LIFE = 10f;
    public float DAMAGE = 2f;
    public float ATACKSPEED = 6f;     //mientras menor sea, mas rapido
    public float RUNSPEED = 2f;
    public ColeccionPuntos pathFinding;
	public GameObject dropBala;
	public GameObject dropVida;
	
	[HideInInspector]
	public float time;
	[HideInInspector]
	public Vector3[] destinos;
    [HideInInspector]
    public Vector3 destino;
    [HideInInspector]
    public Vector3 posicion;
    [HideInInspector]
    public int contador = 0;
    [HideInInspector]
    public NavMeshAgent controller;

    [HideInInspector]
    public Transform enemigoActual;
    [HideInInspector]
    public ArrayList listaEnemigosAtacantes;
	private TorretaControl torreControl;
	private PlayerControl playerControl;
	private HospitalControl hospitalControl;
	private Transform player;
	private bool playerCerca = false;
	private bool hospitalCerca = false;
	private bool muerto=false;
	
	void Start () 
    {
        this.destinos = this.pathFinding.m_puntos;
        this.controller = this.GetComponent<NavMeshAgent>();
        this.controller.speed = RUNSPEED;
        this.updateCalledIn = StateBehaviourMethodsName.FixedUpdate;
		this.listaEnemigosAtacantes = new ArrayList();        
        this.State = StateVampRun.instance;
	}

    public override void OnUpdate()
    {
    }

    public override VampiroControl GetBehaviuor()
    {
        return this;
    }

    void OnTriggerEnter(Collider other)
    {
		// trigger torreta
        if (other.name == "TriggerEnemy")
        {
            enemigoActual = other.GetComponent<Transform>().parent;
            if(!muerto){
				this.State = StateVampAtack.instance;
			}
        }
		// trigger player visto
		else if (other.name == "TriggerVisto")
		{
			player = other.GetComponent<Transform>().parent;
			playerCerca = true;
		}
		// trigger player melee
		else if (other.name == "TriggerMelee")
		{
			enemigoActual = other.GetComponent<Transform>().parent;
			if(!muerto){
            	this.State = StateVampAtack.instance;
			}
		}
		// trigger iglesia
		else if (other.name == "TriggerHospital")
		{
			enemigoActual = other.GetComponent<Transform>().parent;
			hospitalCerca = true;
			if(!muerto){
            	this.State = StateVampAtack.instance;
			}
		}
    }
	
	void OnTriggerExit(Collider other)
    {
		if (other.name == "TriggerVisto")
		{
			playerCerca = false;
		}
		else if (other.name == "TriggerMelee")
		{
			if(!muerto){
			this.State = StateVampRun.instance;
			}
		}
	}
	
  	public void ComprobarDestino()
	{
		Vector3 coorActual = destino - transform.position;		
		
		if (playerCerca)
		{			
			destino = player.position;
		}	
		else
		{
			if (coorActual.magnitude < 1.0f)
			{
				if (contador < destinos.Length - 1)
				{
					contador++;			
				} 
				else
				{
					if(!muerto){
					this.State = StateVampStop.instance;
					}
				}
			}
			
			destino = this.destinos[contador];			
		}	
	}
	
	public void RecibirDmg(float dmg)
	{
        LIFE = LIFE - dmg;
        Debug.Log("------ Vampiro_life_left: " + LIFE);
        if (LIFE <= 0)
        {

            this.State = StateVampDie.instance;
        }
	}

    public void ProducirDmg()
    {
        Debug.Log("VAMPIRO ATACK!!");
        if (enemigoActual != null){
            //enemigoActual.SendMessage("RecibirDmg", DAMAGE);
			if (playerCerca)
			{
				playerControl = enemigoActual.GetComponent("PlayerControl") as PlayerControl;
				playerControl.RecibirDmg(DAMAGE);
			}
			else if (hospitalCerca)
			{
				hospitalControl = enemigoActual.GetComponent("HospitalControl") as HospitalControl;
				hospitalControl.RecibirDmg(DAMAGE);
			}
			else
			{
				torreControl = enemigoActual.GetComponent("TorretaControl") as TorretaControl;
				torreControl.RecibirDmg(DAMAGE);				
			}
		}
    }
	
    public void Destruir()
    {
        Debug.Log("::::::::: destruimos zombie ::::::::::");
		muerto = true;
        animation.Play ("Morir");
        Invoke("Dropear", 4);
		Destroy(this.gameObject,4);
		
    }
	
	public void EnemigoMuerto(Transform tr)
    {
		Debug.Log("vampiro: enemigo muerto --------- " + tr.name);
		if (tr.name == "Turret") 
		{				
			RestoAtacante(tr);
			if (enemigoActual == tr)
	        {	
				if(!muerto){
					
	            	this.State = StateVampRun.instance;
				}
	        }	        
		}
    }
	
	public void Dropear()
	{
		GameObject clonBala = (GameObject) Instantiate(dropBala, transform.position, Quaternion.identity);
		clonBala.GetComponent<PowerUpBalas>().cantidad = 10;
	}
	
	/*
	 * Añade a la lista una torreta que este atacando
	 */
	public void SumoAtacante(Transform atacante)
	{
		listaEnemigosAtacantes.Add(atacante);
	}
	
	/*
	 * Elimina de la lista una torreta que este atacando
	 */
	public void RestoAtacante(Transform atacante)
	{
		if (listaEnemigosAtacantes.Contains(atacante))
        {
            listaEnemigosAtacantes.Remove(atacante);
		}
	}
	
	public void SetID(int spawnID)
	{
	}

}
